package my.android.util.r2;


import java.io.File;
import java.io.InputStream;
import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import android.sax.RootElement;
import android.util.Log;

public class AndroidXML extends java.lang.Object
{
	public static final String DEBUG_TAG = "AndroidXML";

	public static void parseXMLByAndroidSAXAtURL( URL url, RootElement root )
	{
		AndroidSAX sax = new AndroidSAX( url, root );

		sax.parseXMLAtURL( );

		return;
	}

	public static void parseXMLBySAXAtURL( URL url, DefaultHandler handler )
	{
		W3CSAX sax = new W3CSAX( url, handler );

		sax.parseXMLFromURL();
	}

	public static void parseXMLBySAXAtFullPath( String abstract_path_name, DefaultHandler handler )
	{
		/*
		W3CSAX sax = null;

		File file = AndroidFile.exist( abstract_path_name );

		if(file != null )
		{
			sax = new W3CSAX( file.toURI(), handler );
		}
		else
		{
			Log.e(DEBUG_TAG, "can't get xml file instance : "+ abstract_path_name );
		}

		sax.parseXMLFromURI();
		*/
	}

	public static void parseXMLBySAXAtPath( String path, String name, DefaultHandler handler )
	{
		/*
		W3CSAX sax = null;

		File file = AndroidFile.exist( path, name );

		if(file != null )
		{
			sax = new W3CSAX( file.toURI(), handler );
		}
		else
		{
			Log.e(DEBUG_TAG, "can't get xml file instance : "+ path + File.separator + name );
		}

		sax.parseXMLFromURI();
		*/
	}
	/*
	public static Document parseXMLByDOMAtPath( String full_path )
	{
		Document document = null;

		DOM dom = new DOM();

		document = dom.parseXMLAtPath( full_path );

		if( document == null )
		{
			Log.e( DEBUG_TAG, "can't parse this file : " + full_path );
		}

		return document;
	}
	 */	
	public static Document parseXMLByDOMAtPath( String path, String name )
	{
		Document document = null;

		DOM dom = new DOM();

		document = dom.parseXMLAtPath( path, name  );

		if( document == null )
		{
			Log.e( DEBUG_TAG, "can't parse this file : " + path + File.separator + name );
		}

		return document;
	}

	public static Document parseXMLByDOMAtURL( String url )
	{
		Document document = null;

		DOM dom = new DOM();

		document = dom.parseXMLFromURL( url );

		if( document == null )
		{
			Log.e( DEBUG_TAG, "can't parse this url : " + url );
		}

		return document;
	}


}

class W3CSAX
{
	public static final String DEBUG_TAG = "W3CSAX";

	private URI uri = null;

	private DefaultHandler hanlder = null;

	private URL url = null;

	public W3CSAX( URL url, DefaultHandler handler )
	{
		this.url = url;
		this.hanlder = handler;

	}

	public W3CSAX( URI uri, DefaultHandler handler) 
	{ 
		this.uri = uri;
		this.hanlder = handler;
	}

	public void parseXMLFromURL()
	{
		SAXParserFactory parserModel = SAXParserFactory.newInstance();

		SAXParser concreteParser = null;

		try 
		{
			concreteParser = parserModel.newSAXParser();

			XMLReader myReader = null;

			myReader = concreteParser.getXMLReader();

			myReader.setContentHandler( this.hanlder );

			myReader.parse( new InputSource( this.url.openStream() ) );

		} 
		catch (ParserConfigurationException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (SAXException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 

		return;

	}

	public void parseXMLFromURI()
	{
		SAXParserFactory parserModel = SAXParserFactory.newInstance();

		SAXParser concreteParser = null;

		try 
		{
			concreteParser = parserModel.newSAXParser();

			XMLReader myReader = null;

			myReader = concreteParser.getXMLReader();

			myReader.setContentHandler( this.hanlder );

			myReader.parse( new InputSource(uri.toString()) );

		} 
		catch (ParserConfigurationException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (SAXException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}
	}
}

class DOM
{
	public static final String DEBUG_TAG = "DOM";

	public Document parseXMLFromURL( String http_url )
	{
		URL url = null;
		InputStream is = null;
		Document document = null;

		try 
		{
			url = new URL( http_url );

			is = url.openStream();
		} 
		catch (MalformedURLException e1) 
		{
			Log.e(DEBUG_TAG, e1.getMessage());
			e1.printStackTrace();
			return null;
		}
		catch (IOException e1) 
		{
			Log.e(DEBUG_TAG, e1.getMessage());
			e1.printStackTrace();
			return null;
		}

		try 
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			DocumentBuilder db = dbf.newDocumentBuilder();

			document = db.parse( new InputSource(is) );

		} 
		catch ( MalformedURLException e ) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (ParserConfigurationException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (SAXException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}

		return document;
	}

	public Document parseXMLAtPath( String path, String name )
	{
		/*
		Document document = null;

		try 
		{
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

			DocumentBuilder db = dbf.newDocumentBuilder();

			document = db.parse( AndroidFile.exist(path, name) );

		} 
		catch (MalformedURLException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (ParserConfigurationException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (SAXException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}
	
		return document;
		*/
		return null;
	}

}

class AndroidSAX
{
	public static final String DEBUG_TAG = "AndroidSAX";

	private URL url = null;

	private RootElement root = null;

	public AndroidSAX( URL url, RootElement root )
	{
		this.url = url;
		this.root = root;
	}

	public void parseXMLAtURL()
	{
		SAXParserFactory parserModel = SAXParserFactory.newInstance();

		SAXParser concreteParser = null;

		try 
		{
			concreteParser = parserModel.newSAXParser();

			XMLReader myReader = null;

			myReader = concreteParser.getXMLReader();

			myReader.setContentHandler( this.root.getContentHandler() );

			myReader.parse( new InputSource( this.url.openStream() ) );

		} 
		catch ( ParserConfigurationException e ) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		} 
		catch ( SAXException e ) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}
		catch ( IOException e ) 
		{
			Log.e( DEBUG_TAG, e.getMessage() );
			e.printStackTrace();
		}
		
		return;
	}
}
